//
//  GJFCaseCollectViewController.m
//  Wedding
//
//  Created by Marx on 2017/1/14.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFCaseCollectViewController.h"
#import "GJFPlotModel.h"
#import "GJFPlotTableViewCell.h"
#import "GJFCaseCollectModel.h"
#import "GJFPlotDetailViewController.h"

@interface GJFCaseCollectViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray<GJFCaseCollectModel *> *dataArr;
@end

@implementation GJFCaseCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"案例收藏";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-返回"] style:UIBarButtonItemStyleDone target:self action:@selector(popVc)];
    [self tableView];
    [self getPlotData];
}
- (void)popVc {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 婚礼策划列表的网络请求
- (void)getPlotData {
    [GJFNetworkTool POSTRequestWithUrl:APP_MINE_CASECOLLECT params:@{} complete:^(NSDictionary *result) {
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.dataArr = [GJFCaseCollectModel mj_objectArrayWithKeyValuesArray:array];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - 有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
#pragma mark - 每个分区中cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.dataArr[section] data] count];
}

#pragma mark - TableView分区中每个row的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFPlotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GJFPlotTableViewCell"];
    cell.plotModel = [self.dataArr[indexPath.section] data][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFPlotDetailViewController *vc = [[GJFPlotDetailViewController alloc] init];
//    GJFPlotModel *model
    vc.plotId = [[[self.dataArr[indexPath.section] data] objectAtIndex:indexPath.row] id];;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - TableView每个分区中row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 275 * YY;
}
#pragma mark - TableView每个分区头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40 * YY;
}
#pragma mark - TableView每个分区尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10 * YY;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.dataArr[section] title];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[GJFPlotTableViewCell class] forCellReuseIdentifier:@"GJFPlotTableViewCell"];
        [self.view addSubview:_tableView];
        _tableView.mj_header = [GJFRefreshHeader headerWithRefreshingBlock:^{
            [self getPlotData];
        }];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
